import { setToken, getToken, removeToken } from '@/utils/auth.js'
import { login, getData } from '@/api/user'
const state = {
  token: getToken(),
  userdata: {},
  dongRouter: []
}

const getters = {
}

const mutations = {
  setToken(state, token) {
    state.token = token
  },
  setData(state, user) {
    state.userdata = user
  },
  updongRouter(state, router) {
    state.dongRouter = router
  }

}

const actions = {
  // context上下文，传入参数
  async login(context, data) {
    const result = await login(data)
    // console.log(result)
    if (result.success === true) {
      const token = result.data
      context.commit('setToken', token)
      setToken(token)
    }
  },
  async getData(context) {
    const result = await getData()
    // console.log(result)
    context.commit('setData', result.data)
  },
  logout(context) {
    // 清空vuex中token
    context.commit('setToken', '')
    // 清空cookie中的token
    removeToken()
    // 清空vuex中的用户信息
    context.commit('setData', {})
  }

}
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
